<template>
  <div ref="cirularRef" style="width: 100%; height: 100%"></div>
</template>
<script lang="ts" setup>
import { ref, watchEffect } from "vue";
const cirularRef = ref(null) as any;
//  按需引入 echarts
import * as echarts from "echarts";
// onMounted(() => {
//   init();
// });
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  data: {
    type: Array,
    default: () => [],
  },
});
watchEffect(() => {
  if (props.data && cirularRef.value) {
    init();
  }
});
function init() {
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(cirularRef.value);
  // 指定图表的配置项和数据
  var option = {
    title: {
      text: props.title,
    },
    tooltip: {
      trigger: "item",
    },
    // legend: {
    //   top: "5%",
    //   left: "center",
    // },
    grid: {
      top: "5%",
      left: "3%",
      right: "3%",
      bottom: "3%",
      containLabel: true,
    },
    series: [
      {
        name: "Access From",
        type: "pie",
        radius: ["40%", "70%"],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: "center",
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: "bold",
          },
        },
        labelLine: {
          show: false,
        },
        data: props.data,
      },
    ],
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}
</script>
<style scoped></style>
